import { lodashMap, lodashUniqBy } from '@/utils/lodashUtil';

export function getUrlParam(parameterName, currentUrl = location.href) {
    var rs = new RegExp('(^|[&,?])' + parameterName + '=([^\&]*)(\&|$)', 'gi')
      .exec(currentUrl), tmp;
    if (tmp = rs) return tmp[2];
    return null;
  };

  
/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone');
  }
  const targetObj = source.constructor === Array ? [] : {};
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys]);
    } else {
      targetObj[keys] = source[keys];
    }
  });
  return targetObj;
}


export function getDomain(options) {
  if(location.href?.indexOf('localhost')>=0){
    return ''
  } else if (import.meta.env.VITE_MODE == 'development') {
    return 'dev.szqerp.com'
  }
  return '.szqerp.com'
}

export function safeParseJson(str) {
  try {
    if (str == '') return ''
    return JSON.parse(str)
  } catch (e) {
    return undefined
  }
  
}
export function getNavIdByFullpath(url) {
  if (!url) {
    url = location.href;
  }
  var navId = getUrlParam('navid', url);
  if (!navId) {
    navId = getUrlParam('navid', url);
  }
  
  if(navId==='undefined')return ''
  return navId;
}


/**
 * 给路径设置新增一个参数
 * */
export function setNavIdtoPath(url, navId) {
  if (url.indexOf('menu_') >= 0) {
    return url;
  }
  if (!navId) {
    return url;
  }
  var _navId = getNavIdByFullpath(url);
  if (_navId) { //路径已经存在 不用在添加
    return url;
  }
  var index1 = url.indexOf('?');
  if (navId) {
    if (index1 >= 0) {
      url = url + `&navId=${navId}`;
    } else {
      url = url + `?navId=${navId}`;
    }
  }
  
  return url;
}
export function clearZero(obj, ignoreList = []) {
  if (!obj) return obj
  lodashMap(obj, (value, key) => {
    if (ignoreList.includes(key)) {
    } else {
      if (value == 0) {
        obj[key] = undefined
      }
    }
    
  })
  return obj
  
}


export function dateFormatYYYYMMDDHH(value) {
  if (!value) return ''
  value = value.toString()
  if (value && value.indexOf('1970-01-01') >= 0) {
    return ''
  }
  var format = 'YYYY-MM-DD:HH'
  
  
  return moment(value).format(format)
}
export function dateFormatMMDDHHmm(value,_format='MM-DD HH:mm') {
  if (!value) return ''
  value = value.toString()
  if (value && value.indexOf('1970-01-01') >= 0) {
    return ''
  }
  
  var format =_format|| 'MM-DD HH:mm'
  
  
  return moment(value).format(format)
}

export function dateFormatMMDD(value, dateformatstr = 'MM-DD') {
  if (!value) return ''
  value = value.toString()
  if (value && value.indexOf('1970-01-01') >= 0) {
    return ''
  }
  
  return moment(value).format(dateformatstr)
}

export function dateFormatYYYYMMDDHHmm(value) {
  if (!value) return ''
  value = value.toString()
  if (value && value.indexOf('1970-01-01') >= 0) {
    return ''
  }
  var format = 'YYYY-MM-DD HH:mm'
  
  return moment(value).format(format)
}

export function dateFormatYYYYMM(value) {
  if (!value) return ''
  value = value.toString()
  if (value && value.indexOf('1970-01-01') >= 0) {
    return ''
  }
  var format = 'YYYY-MM'
  
  
  return moment(value).format(format)
}
export function dateFormatYYYYMMDD(value) {
  if (!value) return ''
  value = value.toString()
  if (value && value.indexOf('1970-01-01') >= 0) {
    return ''
  }
  var format = 'YYYY-MM-DD'
  
  return moment(value).format(format)
}

export function isIOS() {
  var userAgent = navigator.userAgent
  var _isiOS = userAgent.includes('iPhone') || userAgent.includes('iPad')
  return _isiOS
}

export function uniqBy(array, key) {
  return lodashUniqBy(array, key)
}

export function dateFormatYYYYMMDDHHmmss(value,stict=false) {
  if (!value) return ''
  value = value.toString()
  if (value && value.indexOf('1970-01') >= 0) {
    return ''
  }
  var format = 'YYYY-MM-DD HH:mm:ss'
  return moment(value).format(format)
}

